package com.cloudptt.api.product.func.addr;

import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.message.NotifyNewGroupListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrPriOperator;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.ServerInfo;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.op.AddrDbOperator;
import com.mcptt.http.operator.IHttpAddrOpCb;

import android.content.Context;

import java.util.Iterator;
import java.util.List;

public class AddrFunc  implements IFunc,IHttpAddrOpCb,CallBack
{
	public final static String Log_Key = "AddrFunc";
	private AddrBroadcast broadcast = null;
	private AddrDbOperator dbOperator = null;
	private SysConfig config = null;
	private IAddrDefaultOp defaultOp = null;
	private AddrPriOperator op = null;
	private AddrListener listener = null;
	private boolean isFirstLogined = true;

	public AddrFunc(Context ctx,SysConfig config,IAddrDefaultOp defaultOp,AddrPriOperator op,AddrDbOperator dbOperator)
	{
		this.broadcast = new AddrBroadcast(ctx,this);
		this.config = config;
		this.defaultOp = defaultOp;
		this.op = op;
		this.dbOperator = dbOperator;
	}

	public void setListener(AddrListener listener) {
		this.listener = listener;
	}

	public void setNewGroupListener(NotifyNewGroupListener newGroupListener) {
		this.broadcast.setNewGroupListener(newGroupListener);
	}

	@Override
	public void onLogined(McIdInfo mcIdInfo,ServerInfo info,boolean isLoginSuc) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "onLogined info = " + info + " isFirstLogined = " + isFirstLogined);
		if(isFirstLogined)
		{
			loadAddress();
			isFirstLogined = false;
		}
	}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.d(Log_Key, "doNetworkChange isConn = " + isConn);
	}
	@Override
	public void doForceDetach()
	{
		Log.d(Log_Key, "doForceDetach");
		isFirstLogined = true;
	}
	@Override
	public void doLogout() {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "doLogout");
		isFirstLogined = true;
	}
	@Override
	public void doExit()
	{
		this.broadcast.clearResource();
		this.broadcast = null;
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "doLogout isconnected = " + isconnected);
//		if(Product.getProductType() != ProductConst.ProductType_NAPP_RADIO)
//		{
//			if(!isconnected)
//			{
//				dbOperator.cleanAllLogin();
//			}
//			else
//			{
//				if(info != null)
//				{
//					String svrIp = info.getServerIp();
//					op.loadOnlineState(svrIp, this);
//				}
//			}
//		}
	}



	@Override
	public void onAddrUptResult(boolean isSuc) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "onAddrUptResult isSuc = " + isSuc);
		if(isSuc)
		{
			setDefaultGroupInfo();

			if(listener != null)
			{
				listener.onAddrUpt();
			}
		}
	}

	private void setDefaultGroupInfo()
	{
		List<AddrInfo> list = dbOperator.getAddrList(GroupInfo.Group_Id_Root);
		if(list != null
				&& list.size() > 0)
		{
			String num =config.getDefaultGroupNum();
			Log.d(Log_Key," setDefaultGroupInfo num = " + num);
			if(num != null
					&& num.length() > 0
					&& !num.equals(GroupInfo.Group_Num_All))
			{
				for(Iterator<AddrInfo> itor = list.iterator(); itor.hasNext();)
				{
					AddrInfo info = itor.next();
					if(info != null
							&& info.isGroupType()
							&& info.isSameOne(num))
					{
						defaultOp.setDefaultGroupInfo(info);
					}
				}
			}
			else if(num != null
					&& num.length() > 0
					&& num.equals(GroupInfo.Group_Num_All))
			{
				Log.d(Log_Key," setDefaultGroupInfo else ");
				for(Iterator<AddrInfo> itor = list.iterator();itor.hasNext();)
				{
					AddrInfo info = itor.next();
					Log.d(Log_Key," setDefaultGroupInfo else info = " + info);
					if(info != null
							&& info.isGroupType())
					{
						defaultOp.setDefaultGroupInfo(info);
						return;
					}
				}
			}
		}
	}



	@Override
	public void onOnlineUptResult(boolean isSuc) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "onOnlineUptResult isSuc = " + isSuc);
		if(isSuc
				&& listener != null)
		{
			listener.onOnlineUpt();
		}
	}

	@Override
	public void loadAddress() {
		// TODO Auto-generated method stub
		if(config != null)
		{
			op.loadAddress(config, this);
		}
	}
}
